﻿function setCityPlace(cityUri, countryCityName, placeUri, placeName) {

    $('#cityUri').val(cityUri);
    $('#placeUri').val(placeUri);

    var locationString = countryCityName;
    if (!isNullOrEmpty(placeName))
        locationString += ', ' + placeName;
    $('#eventLocationSpan').html(locationString);
}

function useLabeledPlace(labelId, cityUri, countryCityName, placeUri, placeName) {

    $.get('/event/updategeolabellastusagedatetime', { labelId: labelId });

    setCityPlace(cityUri, countryCityName, placeUri, placeName);

    $.fn.colorbox.close();
}

var cities = null;

function getCities() {

    $('#citiesListBox').loading(true, { align: 'center', img: '/Content/jquery.loading.1.6.3/small-loader.gif' });

    $('#label').empty();
    resetCitiesListBox();
    $('#citiesListBox').empty();

    $.post("/event/getcities?query=" + escape($('#city').val()), null, function (data) {

        cities = data;

        if (cities.length == 0)
            $('#citiesListBox').append('<option>' + noCityWasFoundText + '</option>');
        else {
            for (var i in cities) {
                var city = cities[i];
                $('#citiesListBox').append('<option value="' + city.Uri + '">' + city.CountryName + ', ' + city.Name + '</option>');
            }

            $('#citiesListBox').removeAttr('disabled');
        }

        $('#citiesListBox').loading(false);

    }, "json");
}

function chooseLocation() {

    var cityUri = nullToEmptyString($('#citiesListBox').val());
    var countryCityName = nullToEmptyString($('#citiesListBox option:selected').text());

    var placeUri = nullToEmptyString($('#objectsListBox').val());
    var placeName = nullToEmptyString($('#objectsListBox option:selected').text());

    var geoLabel = nullToEmptyString($('#label').val());

    if (isNullOrEmpty($.trim(geoLabel))) {
        setCityPlace(cityUri, countryCityName, placeUri, placeName);
        $.fn.colorbox.close();
    }
    else {
        $.post('/GeoLabels/AddGeoLabel?geoLabel=' + escape(geoLabel) +
                '&cityUri=' + escape(cityUri) + '&cityName=' + escape(countryCityName) +
                '&placeUri=' + escape(placeUri) + '&placeName=' + escape(placeName), null, function (data) {

                    if (data.Success != null) {
                        setCityPlace(cityUri, countryCityName, placeUri, placeName);
                        $.fn.colorbox.close();
                    }
                    else if (data.Error != null)
                        alert(data.Error);
                }, 'json');
    }
}

function setCityLabel() {
    $('#label').val(currentCity.Name);
}

function setObjectLabel() {
    $('#label').val(currentObject.Name);
}

var objectMarker = null;
var currentCity = null;

function changeCity() {

    resetObjectsTypesListBox();

    var selectedCityUri = $('#citiesListBox').val();

    for (var i in cities) {
        var city = cities[i];
        if (city.Uri == selectedCityUri) {

            currentCity = city;

            var cityLocation = new CM.LatLng(city.Latitude, city.Longitude);

            map.setCenter(cityLocation, 14);

            $('#chooseLocationButton').attr('disabled', false);
            setCityLabel();

            break;
        }
    }
}

function updateObjects() {

    var selectedCityIndex = $('#citiesListBox').attr("selectedIndex");
    if (selectedCityIndex == -1) {
        alert('Please select city first');
        return;
    }

    var mapZoom = map.getZoom();
    if (mapZoom < 14) {
        alert('Sorry, but you can retrieve objects only if map zoom is 14 or more (because other way there are too many objects). ' +
             'Your current zoom is equal to ' + mapZoom + ', please zoom in');
        return;
    }

    $('#objectsTypesListBox').loading(true, { align: 'center', img: '/Content/jquery.loading.1.6.3/small-loader.gif' });

    setCityLabel();
    resetObjectsTypesListBox();
    $('#objectsTypesListBox').empty();

    var southWest = map.getBounds().getSouthWest();
    var northEast = map.getBounds().getNorthEast();

    $.post("/lgd/getnearobjectstypes?westLatitude=" + southWest.lat() +
        "&southLongitude=" + southWest.lng() + "&eastLatitude=" + northEast.lat() +
        "&northLongitude=" + northEast.lng(), null, function (data) {

            if (data.length == 0)
                $('#objectsTypesListBox').append('<option>' + noObjectsInsideCurrentMapRegionText + '</option>');
            else {
                for (var i in data) {
                    var objectType = data[i];
                    $('#objectsTypesListBox').append('<option value="' + objectType.Uri + '">' + objectType.Label + '</option>');
                }

                $('#objectsTypesListBox').removeAttr('disabled');
            }

            $('#objectsTypesListBox').loading(false);

        }, "json");
}

var objects = null;

function changeObjectsType() {

    $('#objectsListBox').loading(true, { align: 'center', img: '/Content/jquery.loading.1.6.3/small-loader.gif' });

    resetObjectsListBox();

    var southWest = map.getBounds().getSouthWest();
    var northEast = map.getBounds().getNorthEast();

    $.post("/event/getnearobjects?objectTypeUri=" + escape($('#objectsTypesListBox').val()) +
            "&westLatitude=" + southWest.lat() +
            "&southLongitude=" + southWest.lng() +
            "&eastLatitude=" + northEast.lat() +
            "&northLongitude=" + northEast.lng(), null, function (data) {

                objects = data;

                $('#objectsListBox').empty();

                for (var i in objects) {
                    var object = objects[i];
                    $('#objectsListBox').append('<option value="' + object.Uri + '">' + object.Name + '</option>');
                }
                $('#objectsListBox').removeAttr('disabled');

                $('#objectsListBox').loading(false);

            }, "json");
}

var currentObject = null;

function changeObject() {
    var selectedObjectUri = $('#objectsListBox').val();

    for (var i in objects) {
        var object = objects[i];
        if (object.Uri == selectedObjectUri) {

            currentObject = object;

            var objectLocation = new CM.LatLng(object.Latitude, object.Longitude);

            if (objectMarker != null)
                objectMarker.setLatLng(objectLocation);
            else {
                objectMarker = new CM.Marker(objectLocation, { title: "Selected object", clickable: false });

                map.addOverlay(objectMarker);
            }

            objectMarker.show();

            setObjectLabel();

            break;
        }
    }
}

function resetCitiesListBox() {

    if (map != null)
        map.setCenter(new CM.LatLng(0, 0), 0);

    $('#citiesListBox').attr('disabled', true);
    $('#citiesListBox').empty();
    $('#citiesListBox').append('<option value="noSelection">' + enterCityText + '</option>');

    $('#chooseLocationButton').attr('disabled', true);

    resetObjectsTypesListBox();
}

function resetObjectsTypesListBox() {
    $('#objectsTypesListBox').attr('disabled', true);
    $('#objectsTypesListBox').empty();
    $('#objectsTypesListBox').append('<option value="noSelection">' + updateObjectsFirstText + '</option>');



    resetObjectsListBox();
}

function resetObjectsListBox() {
    $('#objectsListBox').attr('disabled', true);
    $('#objectsListBox').empty();
    $('#objectsListBox').append('<option value="no selection">' + chooseObjectTypeText + '</option>');

    if (objectMarker != null)
        objectMarker.hide();
}